//
//  KKTopItemCell.m
//  DoMi
//
//  Created by 康凯 on 2016/12/16.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKTopItemCell.h"
#import "KKAllModuleDialog.h"
#import "KKStudyItem.h"
#import "KKItemListController.h"

@implementation KKTopItemCell

- (void)awakeFromNib {
    [super awakeFromNib];
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(allClick)];
    [self.allView addGestureRecognizer:tap];
}

-(void)initData:(NSArray *)items{
    if(items!=nil&&items.count>0){
        self.items=items;
//        KKStudyItem *item=items[0];
//        [self.firstBtn setTitle:item.title forState:UIControlStateNormal];
//        [self.firstBtn addTarget:self action:@selector(firstBtnClick) forControlEvents:UIControlEventTouchUpInside];
        self.scrollWidth=0;
        for(int i=0;i<items.count;i++){
            if(i==1){
                UIView *divider=[[UIView alloc] initWithFrame:CGRectMake(self.scrollWidth+5, 7, 1, 16)];
                divider.backgroundColor=[UIColor grayColor];
                [self.scrollView addSubview:divider];
                self.scrollWidth+=11;
            }
            KKStudyItem *item=items[i];
            CGSize textSize=[KKAppUtils sizeWithText:item.title font:[UIFont systemFontOfSize:14] maxSize:CGSizeMake(1000, 30)];
            UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(self.scrollWidth,0, textSize.width+15, 30)];
            self.scrollWidth+=textSize.width+15;
            btn.tag=i;
//            btn.titleLabel.font=[UIFont boldSystemFontOfSize:14];
            btn.titleLabel.font=[UIFont systemFontOfSize:14];
            [btn setTitle:item.title forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor colorWithHexString:@"#787879"] forState:UIControlStateNormal];
//            btn.titleLabel.font=[UIFont systemFontOfSize:14];
            [self.scrollView addSubview:btn];
            [btn addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        self.scrollView.contentSize = CGSizeMake(self.scrollWidth, 30);

    }
    
}
#pragma mark 按钮--全部
-(void)allClick{
    KKAllModuleDialog *allModule=[[[NSBundle mainBundle] loadNibNamed:@"KKAllModuleDialog" owner:self options:nil] lastObject];
    allModule.items=self.items;
    allModule.studyVC=self.studyVC;
    [allModule show];
}
#pragma mark 按钮--第一个item点击
-(void)firstBtnClick{
    KKStudyItem *item= self.items[0];
    KKItemListController *itemView=[[KKItemListController alloc] init];
    itemView.codeId=item.code;
    itemView.barTitle=item.title;
    [[KKAppUtils getCurrentVisiableVC:self].navigationController pushViewController:itemView animated:YES];
}
#pragma mark 按钮--item点击
-(void)itemClick:(UIButton *)btn{
    KKStudyItem *item= self.items[btn.tag];
    KKItemListController *itemView=[[KKItemListController alloc] init];
    itemView.codeId=item.code;
    itemView.barTitle=item.title;
    [[KKAppUtils getCurrentVisiableVC:self].navigationController pushViewController:itemView animated:YES];
}

@end
